<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        iframe {
            width: 80%;
            height: 600px;
        }
    </style>
</head>

<body>
    <button id="btn1">在新窗口打开</button>
    <button id="btn2">在本窗口打开</button>
    <button id="btn3">在iframe打开</button>
    <button id="btn4">关闭网页</button>


    <div>
        <iframe src="news.html" frameborder="0" name="iframeA" id="iframe1"></iframe>
    </div>
    <div>
        <iframe src="http://www.so.com" frameborder="0" name="iframeB" id="iframe2"></iframe>
    </div>
    <div id="selText">
        <pre>曾经沧海难为水，除却巫山不是云。</pre>
        <pre>取次花丛懒回顾，半缘修道半缘君。</pre>
    </div>
    <p id="selRes">您选中的内容是：</p>
    <script>
        var h = innerHeight;
        window.onresize = function() {
            this.console.log(innerHeight)
        }
        var btn1 = document.getElementById('btn1');
        var btn2 = document.getElementById('btn2');
        var btn3 = document.getElementById('btn3');
        var btn4 = document.getElementById('btn4');
        //定义一个全局变量，可以让每个函数都访问和修改
        var page;

        btn1.addEventListener('click', function() {
            page = open("https://baidu.com", "_blank")
        })

        btn2.onclick = function() {
            open("https://baidu.com", "_self")
        }

        btn3.onclick = function() {
            open("https://baidu.com", "iframeA");
            console.log(page)
        }

        //关闭网页
        btn4.addEventListener('click', function() {
            page.close()
        })

        var res = document.getElementById('selRes');

        document.body.addEventListener('mouseup', function() {
                var selObj = getSelection();

                res.innerText = `您选中的内容是:${selObj.toString()}`
            })
            //  根据iframe的name值，获取整个iframe的window对象
        var pageA = window['iframeA']
        var pageB = frames['iframeB']
            //通过iframe的dom去获取他本身的window对象
        var domA = document.querySelector('#iframe1');
        var pageA1 = domA.contentWindow
    </script>
</body>

</html>